﻿package morn.core.components
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class Panel extends Container implements IContent, ISkinAbleContainer
    {
        protected var _content:Box;
        protected var _vScrollBar:VScrollBar;
        protected var _hScrollBar:HScrollBar;
        protected var _barPosition:Boolean = false;
        protected var _offsetV:int = 0;
        protected var _offsetH:int = 0;
        private var _skinChanged:Boolean = false;

        public function Panel()
        {
            var _loc_1:int = 100;
            this.height = 100;
            this.width = _loc_1;
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.vScrollBarSkin = param1 as String;
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new Box();
            this._content = new Box();
            super.addChild(_loc_1);
            return;
        }// end function

        override public function addChild(param1:DisplayObject) : DisplayObject
        {
            callLater(this.changeScroll);
            return this._content.addChild(param1);
        }// end function

        override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject
        {
            callLater(this.changeScroll);
            return this._content.addChildAt(param1, param2);
        }// end function

        override public function removeChild(param1:DisplayObject) : DisplayObject
        {
            callLater(this.changeScroll);
            return this._content.removeChild(param1);
        }// end function

        override public function removeChildAt(param1:int) : DisplayObject
        {
            callLater(this.changeScroll);
            return this._content.removeChildAt(param1);
        }// end function

        override public function removeAllChild(param1:DisplayObject = null) : void
        {
            var _loc_2:* = this._content.numChildren - 1;
            while (_loc_2 > -1)
            {
                
                if (param1 != this._content.getChildAt(_loc_2))
                {
                    this._content.removeChildAt(_loc_2);
                }
                _loc_2 = _loc_2 - 1;
            }
            callLater(this.changeScroll);
            return;
        }// end function

        override public function getChildAt(param1:int) : DisplayObject
        {
            return this._content.getChildAt(param1);
        }// end function

        override public function getChildByName(param1:String) : DisplayObject
        {
            return this._content.getChildByName(param1);
        }// end function

        override public function getChildIndex(param1:DisplayObject) : int
        {
            return this._content.getChildIndex(param1);
        }// end function

        override public function get numChildren() : int
        {
            return this._content.numChildren;
        }// end function

        public function changeScroll() : void
        {
            var _loc_4:Number = NaN;
            var _loc_1:* = this._vScrollBar && this._content.height > _height;
            var _loc_2:* = this._hScrollBar && this._content.width > _width;
            var _loc_3:* = _loc_1 ? (_width - (this._barPosition ? (0) : (this._vScrollBar.width))) : (_width);
            _loc_4 = _loc_2 ? (_height - (this._barPosition ? (0) : (this._hScrollBar.height))) : (_height);
            this._content.scrollRect = new Rectangle(0, 0, _loc_3, _loc_4);
            if (this._vScrollBar)
            {
                this._vScrollBar.x = this._barPosition ? (0) : (_width - this._vScrollBar.width);
                this._vScrollBar.y = 0;
                this._vScrollBar.height = _height + this._offsetV;
                this._vScrollBar.scrollSize = this._content.height * 0.1;
                this._vScrollBar.thumbPercent = _loc_4 / this._content.height;
                this._vScrollBar.setScroll(0, this._content.height - _loc_4, this._vScrollBar.value);
                if (this._content.height > _height)
                {
                    this._vScrollBar.disabled = false;
                }
                else
                {
                    this._vScrollBar.disabled = true;
                }
            }
            if (this._hScrollBar)
            {
                this._hScrollBar.x = 0;
                this._hScrollBar.y = _height - this._hScrollBar.height;
                this._hScrollBar.width = _width;
                this._hScrollBar.thumbPercent = _loc_3 / this._content.width;
                this._hScrollBar.setScroll(0, this._content.width - _loc_3, this._hScrollBar.value);
                if (this._content.width > _width)
                {
                    this._hScrollBar.disabled = false;
                }
                else
                {
                    this._hScrollBar.disabled = true;
                }
            }
            this.createContentBg();
            return;
        }// end function

        private function createContentBg() : void
        {
            var _loc_1:* = this._content.graphics;
            _loc_1.clear();
            _loc_1.beginFill(16776960, 0);
            _loc_1.drawRect(0, 0, this._content.width, this._content.height);
            _loc_1.endFill();
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            callLater(this.changeScroll);
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            callLater(this.changeScroll);
            return;
        }// end function

        public function get vScrollBarSkin() : String
        {
            return this._vScrollBar.skin;
        }// end function

        public function set vScrollBarSkin(param1:String) : void
        {
            if (this._vScrollBar == null)
            {
                var _loc_2:* = new VScrollBar();
                this._vScrollBar = new VScrollBar();
                super.addChild(_loc_2);
                this._vScrollBar.addEventListener(Event.CHANGE, this.onScrollBarChange);
                this._vScrollBar.target = this;
                callLater(this.changeScroll);
            }
            this._vScrollBar.skin = param1;
            this._skinChanged = true;
            return;
        }// end function

        public function get hScrollBarSkin() : String
        {
            return this._hScrollBar.skin;
        }// end function

        public function set hScrollBarSkin(param1:String) : void
        {
            if (this._hScrollBar == null)
            {
                var _loc_2:* = new HScrollBar();
                this._hScrollBar = new HScrollBar();
                super.addChild(_loc_2);
                this._hScrollBar.addEventListener(Event.CHANGE, this.onScrollBarChange);
                this._hScrollBar.target = this;
                callLater(this.changeScroll);
            }
            this._hScrollBar.skin = param1;
            return;
        }// end function

        public function get vScrollBar() : ScrollBar
        {
            return this._vScrollBar;
        }// end function

        public function get hScrollBar() : ScrollBar
        {
            return this._hScrollBar;
        }// end function

        public function get content() : Sprite
        {
            return this._content;
        }// end function

        protected function onScrollBarChange(event:Event) : void
        {
            var _loc_3:ScrollBar = null;
            var _loc_4:int = 0;
            var _loc_2:* = this._content.scrollRect;
            if (_loc_2)
            {
                _loc_3 = event.currentTarget as ScrollBar;
                _loc_4 = Math.round(_loc_3.value);
                if (_loc_3.direction == ScrollBar.VERTICAL)
                {
                    _loc_2.y = _loc_4;
                }
                else
                {
                    _loc_2.x = _loc_4;
                }
                this._content.scrollRect = _loc_2;
            }
            return;
        }// end function

        override public function commitMeasure() : void
        {
            exeCallLater(this.changeScroll);
            return;
        }// end function

        public function scrollTo(param1:Number = 0, param2:Number = 0) : void
        {
            this.commitMeasure();
            if (this.vScrollBar)
            {
                this.vScrollBar.value = param2;
            }
            if (this.hScrollBar)
            {
                this.hScrollBar.value = param1;
            }
            return;
        }// end function

        public function set barPosition(param1:Boolean) : void
        {
            this._barPosition = param1;
            return;
        }// end function

        public function set offsetV(param1:int) : void
        {
            this._offsetV = param1;
            return;
        }// end function

        public function set offsetH(param1:int) : void
        {
            this._offsetH = param1;
            return;
        }// end function

    }
}
